package com.gali.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/**
 * @Auther: gali
 * @Date: 2022-08-24 15:21
 * @Description:
 */
class ClickNumber{
    int number=0;
    public synchronized void clickAdd(){
        number++;
    }
    AtomicLong atomicLong = new AtomicLong(0);
    public void clickAtomicLong(){
        atomicLong.getAndIncrement();
    }
    LongAdder longAdder = new LongAdder();
    public void clickLongAdder(){
        longAdder.increment();
    }
    LongAccumulator longAccumulator = new LongAccumulator((x,y)->x+y,0);
    public void clickLongAccumulator(){
        longAccumulator.accumulate(1);
    }
}
public class AccumulatorCompareDemo {
    public final static int  _1W=10000;
    public final static int SIZE=50;
    public static void main(String[] args) throws InterruptedException {
        ClickNumber clickNumber = new ClickNumber();
        long startTime;
        long endTime;
        CountDownLatch countDownLatch1 = new CountDownLatch(SIZE);
        CountDownLatch countDownLatch2 = new CountDownLatch(SIZE);
        CountDownLatch countDownLatch3 = new CountDownLatch(SIZE);
        CountDownLatch countDownLatch4 = new CountDownLatch(SIZE);
        startTime = System.currentTimeMillis();
        for (int i = 0; i < SIZE; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j <100*_1W; j++) {
                        clickNumber.clickAdd();
                    }
                } finally {
                    countDownLatch1.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch1.await();
         endTime = System.currentTimeMillis();
        System.out.println("消耗的时间是："+(endTime-startTime)+"毫秒"+"\t clickNumber"+clickNumber.number);


        startTime = System.currentTimeMillis();
        for (int i = 0; i < SIZE; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j <100*_1W; j++) {
                        clickNumber.clickAtomicLong();
                    }
                } finally {
                    countDownLatch2.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch2.await();
        endTime = System.currentTimeMillis();
        System.out.println("消耗的时间是："+(endTime-startTime)+"毫秒"+"\t clickNumber"+clickNumber.atomicLong.get());


        startTime = System.currentTimeMillis();
        for (int i = 0; i < SIZE; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j <100*_1W; j++) {
                        clickNumber.clickLongAdder();
                    }
                } finally {
                    countDownLatch3.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch3.await();
        endTime = System.currentTimeMillis();
        System.out.println("消耗的时间是："+(endTime-startTime)+"毫秒"+"\t clickNumber"+clickNumber.longAdder.sum());


        startTime = System.currentTimeMillis();
        for (int i = 0; i < SIZE; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j <100*_1W; j++) {
                        clickNumber.clickLongAccumulator();
                    }
                } finally {
                    countDownLatch4.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch4.await();
        endTime = System.currentTimeMillis();
        System.out.println("消耗的时间是："+(endTime-startTime)+"毫秒"+"\t clickNumber"+clickNumber.longAccumulator.get());
    }
}
